<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Evaluateur
 *
 * @author guillaume
 */
class Evaluateur {

    protected $questions;
    protected $qrelDocuments;
    protected $relevantDocuments;
    protected $pertinences;
    protected $steps;
    
    public function __construct(PDO $db) {
        $this->db               = $db;
        $this->steps            = array();
        $this->pertinences      = array();
        $this->relevantDocuments      = array();
        $this->qrelDocuments    = array();
    }

    public function setSteps($steps)
    {
        $this->steps = $steps;

        return $this;
    }

    public function analyse()
    {
        $this->pertinences = array();
        foreach($this->questions as $i => $question)
        {
            //Liste des documents pertinents ayant un score > 0 dans le fichier qrelQXXX.txt
            $this->relevantDocuments[$i]  = $this->getDocumentsFromQRel($question["name"]);
            
            //Liste de tous les documents selectionnés
            $allSelectedDocuments       = array_map(function(indexClass $indClass) {
                return $indClass->getDocument();
            }, search($this->db, $question["description"]));

            $this->pertinences[$i] = (new Pertinence($question, $allSelectedDocuments, $this->relevantDocuments[$i]))->computeNotes($this->steps);
        }

        return $this;
    }

    protected function getDocumentsFromQRel($nameQuestion) {
        $results_file = file(__DIR__ . "/../qrels/" . $nameQuestion, FILE_IGNORE_NEW_LINES);

        $documents = array();
        foreach ($results_file as $document) {
            //Ex: D10.html	0
            $path_and_note = preg_split("/\s/", $document);
            $path = $path_and_note[0];
            $note = $path_and_note[1];
            
            if($note !== "0")
            {
                $documents[] = array("path" => $path, "note" => $note);
            }            
        }

        return $documents;
    }
    
    public function getMoyennePertinences()
    {
        $pertinences             = array();
        $pertinencesQuestions    = array();
        
        foreach($this->steps as $step)
        {
            $precisions = 0;
            $rappels    = 0;
            foreach($this->questions as $i => $question)
            {
                if(! isset($pertinencesQuestions[$i]))
                {
                    $pertinencesQuestions[$i] = array("precision" => 0, "rappel" => 0);
                }                
                
                $note       = $this->pertinences[$i]->getNote($step);
                $rappels    += $note["rappel"];
                $precisions += $note["precision"];
                $pertinencesQuestions[$i]["precision"]   += $note["precision"];
                $pertinencesQuestions[$i]["rappel"]      += $note["rappel"];
            }

            $pertinences[$step] = array("precision" => $precisions / count($this->questions), "rappel" => $rappels / count($this->questions));
        }
        
        foreach($this->questions as $i => $question)
        {
            $pertinencesQuestions[$i]["precision"]  /= count($this->steps);
            $pertinencesQuestions[$i]["rappel"]     /= count($this->steps);
        }
        
        return array($pertinences, $pertinencesQuestions);
    }

    public function getQuestions() {
        return $this->questions;
    }

    public function getPertinences() {
        return $this->pertinences;
    }

    public function setQuestions($questions) {
        $this->questions = $questions;
        return $this;
    }

    public function setPertinences($pertinences) {
        $this->pertinences = $pertinences;
        return $this;
    }

    public function getQrelDocuments() {
        return $this->qrelDocuments;
    }

    public function getDbDocuments() {
        return $this->relevantDocuments;
    }

    public function setQrelDocuments($qrelDocuments) {
        $this->qrelDocuments = $qrelDocuments;
        return $this;
    }

    public function setDbDocuments($relevantDocuments) {
        $this->relevantDocuments = $relevantDocuments;
        return $this;
    }
}
